package com.kly.sc;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.*;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class FcmClient {


    String token="fH30jEwGStSM80bbovbNUZ:APA91bHawA6RwLXtU9bmVjiCELFkTZN9jUAy5zJt8kt6YBUPDBYRiUlzl2CGDgyx_-CuvjKm80taMTz7KmCHAkfvGq2zcpEVO2pQkxewwcjLDWZ5FPcw9E4ZjHXA-KPt8w7U407q1A8z";
    // The JSON string
    public FcmClient() throws IOException {

        InputStream serviceAccount = getClass().getClassLoader().getResourceAsStream("meboo-online.json");

        FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .build();
        FirebaseApp.initializeApp(options);
        System.out.println("Firebase service account key is valid.");
        try {
            Notification notification = new Notification("just a title ","[\"pushMsg\",\"just a push content\"]");


            //type 1 回复消息  2普通照片  3私密照片  4普通视频  5私密视频 6wlm喜欢
            //isNotification true 通知栏消息  false应用内消息
            //targetId 目标的userCode
            //imageUrl 对方发送的图片或者视频链接
            // Build a message
            Message message = Message.builder()
                    .putData("score", "850")
                    .putData("time", "2:45")
                    .putData("score", "850")
                    .putData("time", "2:45")
                    .putData("content", "this is just testing")
                    .putData("type", "1")
                    .putData("isNotification", "false")
                    .putData("targetId", "12313")
                    .putData("imageUrl", "http://xx.jpg")
                    .setToken(token)
                    .setNotification(notification)
                    .build();

// Send a message to the device corresponding to the provided
// registration token.


// Send a message to the device corresponding to the provided
// registration token.
            String response = FirebaseMessaging.getInstance().sendAsync(message).get();
// Response is a message ID string.
            System.out.println("Successfully sent message: " + response);
        }catch (Exception e){
            e.printStackTrace();
        }
    }



    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        new FcmClient();
    }
}

